package com.zipou.myFax.common;

import gnu.hylafax.Client;
import gnu.hylafax.HylaFAXClient;
import gnu.hylafax.Job;
import gnu.inet.ftp.FtpClientProtocol;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.zipou.myFax.MyFax;
import com.zipou.myFax.Exception.FaxInitException;
import com.zipou.myFax.Exception.MalformedNumberException;
import com.zipou.myFax.Exception.SendingFaxException;

public class FaxServiceImpl {

	private Client c = new HylaFAXClient();

	private MyFax myfax;

	public FaxServiceImpl(MyFax myfax) throws FaxInitException {
		try {
			this.myfax = myfax;

			// Open the host.
			c.open(myfax.getProperty("HYLAFAX_SERVER"));
			c.user(myfax.getProperty("HYLAFAX_USER"));
			c.mode(FtpClientProtocol.MODE_ZLIB);
			c.type(FtpClientProtocol.TYPE_IMAGE);

		} catch (Exception e) {
			throw new FaxInitException("Unable to Init the FaxService: "
					+ e.getMessage());
		} 
	}

	public void init(FaxCallback callback) {
		// TODO Auto-generated method stub

	}

	public final Long sendFax(String number, String file) throws SendingFaxException {
		Long id = null;
		try {
			Job job = c.createJob();
			job.setDialstring(this.checkNumber(number));
			job.setNotifyAddress("zipou");
			job.setVerticalResolution(210);
			job.setMaximumDials(1);
			job.setMaximumTries(1);
			job.addDocument(file);
			job.setPageLength(Integer.parseInt(myfax.getProperty("PAGE_LENGTH")));
			job.setPageWidth(Integer.parseInt(myfax.getProperty("PAGE_WIDTH")));
			job.setKilltime("000259");
			c.submit(job);
			id = job.getId();

		} catch (MalformedNumberException e) {
			throw new SendingFaxException(
					"Unable to Send Fax because of a Malfromed Number : "
							+ e.getMessage());
		} catch (Exception e) {
			throw new SendingFaxException(
					"Unknown Error in SendingFax Method: " + e.getMessage());
		}

		return id;
	}

	// public int getState(Long id) {
	// Vector list = new Vector();
	// //Get the list of faxes that have not completed yet.
	// // list.addAll(c.getList("sendq"));
	// Job j = null;
	// try {
	// j = c.getJob(id);
	// return Integer.parseInt(j.getJobInfo());
	//
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// myfax.getLogger().severe("Merdier on Method getState on FaxServiceImpl: "
	// + e.getMessage());
	// myfax.panic(e.getMessage());
	// }
	// return 0;
	// }

//	private void close() throws FaxInitException {
//		try {
//			c.quit();
//		} catch (Exception e) {
//			throw new FaxInitException("Unable to CLOSE the FaxService: "
//					+ e.getMessage());
//		}
//	}

	/**
	 * Check the number format
	 * 
	 * @param number
	 * @return the number formated
	 * @throws ConfigException
	 */
	private String checkNumber(String number) throws MalformedNumberException {

		Pattern p = Pattern.compile(myfax.getProperty("NUMBER_REGEX"));
		Matcher m = p.matcher(number);
		while (m.find()) {
			return number.substring(m.start(), m.end());			
		}
		throw new MalformedNumberException("Number not Found in '" + number
				+ "'");
	}
}